//
//  SelectDate.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/20.
//

#import "SelectDate.h"
#import "BRDatePickerView.h"
@interface SelectDate()
@property (nonatomic,strong)UIView *pView;
@property  (nonatomic,strong)NSString *start;
@property  (nonatomic,strong)NSString *end;
@end
@implementation SelectDate

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI
{
    UIView *view = [UIView new];
    [self addSubview:view];
    view.backgroundColor = UIColor.clearColor;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_offset(0);
    }];
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 454)];
    bgIV.image = [UIImage imageNamed:@"selectDate"];
    [self addSubview:bgIV];
    [self sendSubviewToBack:bgIV];
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    [close setImage:[UIImage imageNamed:@"selectClose"] forState:UIControlStateNormal];
    [self addSubview:close];
    [close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.right.mas_offset(-16);
    }];
    [self addSubview:self.pView];
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectBtn setImage:[UIImage imageNamed:@"selectTurn"] forState:UIControlStateNormal];
    [self addSubview:selectBtn];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-bottomSafeHeight);
        make.left.mas_offset(23);
        make.right.mas_offset(-23);
        make.height.mas_equalTo(52);
    }];
    BRDatePickerView *datePickerView = [[BRDatePickerView alloc]init];
    datePickerView.pickerMode = BRDatePickerModeYMD;
    datePickerView.maxDate = [NSDate date];
    datePickerView.isAutoSelect = YES;
    datePickerView.customUnit = @{@"year": @"", @"month": @"", @"day": @""};
    datePickerView.showUnitType = BRShowUnitTypeAll;
    datePickerView.resultBlock = ^(NSDate *selectDate, NSString *selectValue) {
    };
    NSString *birth = [[NSUserDefaults standardUserDefaults] objectForKey:@"birth"];

    if(birth == nil || [birth isEqualToString:@""]){
        NSDate *currentDate = [NSDate date];
        datePickerView.selectDate = currentDate;
        self.end = [NSDate br_stringFromDate:currentDate dateFormat:@"yyyy-MM-dd"];
    } else {
        datePickerView.selectDate = [NSDate br_dateFromString:birth dateFormat:@"yyyy-MM-dd"];
        self.end = birth;
        [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"birth"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    NSString *format = @"yyyy-MM-dd";
    datePickerView.resultRangeBlock = ^(NSDate * _Nullable selectStartDate, NSDate * _Nullable selectEndDate, NSString * _Nullable selectValue) {
        NSString *startTime = [NSDate br_stringFromDate:selectStartDate dateFormat:format];
        NSString *endTime = [NSDate br_stringFromDate:selectEndDate dateFormat:format];
        self->_start =  startTime;
        self->_end = endTime;
    };
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.pickerColor = UIColor.clearColor;
    customStyle.pickerTextColor = UIColor.blackColor;
    datePickerView.pickerStyle = customStyle;
    [datePickerView addPickerToView:self.pView];
    [close addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [selectBtn addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];
}
-(void)close
{
    if (self.closeAction) {
        self.closeAction();
    }
}

-(void)commit
{
    if (self.dateCommitAction) {
        [[NSUserDefaults standardUserDefaults] setObject:self.end forKey:@"birth"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self.dateCommitAction(self.end);
    }
}

-(UIView *)pView
{
    if (!_pView) {
        _pView = [[UIView alloc]initWithFrame:CGRectMake(0,71 , SCREEN_WIDTH, 454 - 71-52-bottomSafeHeight)];
        _pView.backgroundColor = UIColor.clearColor;
    }
    return _pView;
}

@end
